package com.honglang.module.article.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.honglang.dao.GenericDaoImpl;
import com.honglang.module.article.entity.BlogArticle;

public class BlogArticleDaoImpl extends GenericDaoImpl<BlogArticle,Integer> implements BlogArticleDao{
	
	
	
	/**
	 * get the blogArticles by the articleCategoryId;
	 * @param articleCategoryId
	 * @return
	 */
	public List<BlogArticle> getBlogArticleByArticleCategoryId(Integer articleCategoryId){
		
		DetachedCriteria criteria = DetachedCriteria.forClass(BlogArticle.class);
		criteria.createAlias("article", "article")
		  .createAlias("article.articleCategories", "articleCategories")
		  .add(Restrictions.eq("articleCategories.id", articleCategoryId));

		return this.getByDetachedCriteria(criteria);
	}
	
	
	/**
	 * get the  blogArticles  of one page
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public List<?> getBlogArticlePerPage(int pageSize,int pageNo){
		Session session= getHibernateTemplate().getSessionFactory().openSession();
		Criteria criteria=session.createCriteria(BlogArticle.class);
		int startIndex=pageSize*(pageNo-1);
		criteria.addOrder(Order.desc("id"));
		criteria.setFirstResult(startIndex);
		criteria.setMaxResults(pageSize);
		List<?> result=criteria.list();
		session.close();
		return result;
	}
	
	
	
}
